package mianyang.zhangsan.com.controller;

import mianyang.zhangsan.com.pojo.Admin;
import mianyang.zhangsan.com.pojo.LoginUser;
import mianyang.zhangsan.com.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登出请求
     * @return
     */
    @RequestMapping("/logout")
    public String logout(){
        return "../../login";
    }

    /**
     * 管理员登录请求
     * @param login
     * @param model
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(@RequestBody LoginUser login, Model model){
        String message = "";
        System.out.println(login);
        if (login != null){
            Admin admin = adminService.queryInfoByUsername(login.getUsername());
            if (admin != null && admin.getPassword().equals(login.getPassword())){
                message = "{\"success\":\"true\"}";
                return message;
            }
        }
        message = "{\"success\":\"false\"}";
        return message;
    }

    /**
     * 后台管理系统
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value ="/manager", produces = "application/json;charset=utf-8")
    public String manager(HttpServletRequest request, HttpSession session){
        String referer = request.getHeader("referer");
        String username = request.getParameter("username");
        System.out.println(username);
        //防止直接请求
        if(!referer.equals("http://localhost/login.jsp")){
            return "../../login";
        }
        System.out.println(referer);
        //将当前的管理员信息存入session中
        session.setAttribute("username", username);
        return "main";
    }

    /**
     * 管理员密码修改
     * @return
     */
    @RequestMapping("/modify")
    public String modify(){
        return "password_modify";
    }

}
